<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户中心</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="js/jquery/jquery.min.js"></script>
<script src="js/jquery.validation/1.14.0/jquery.validate.min.js"></script>
<script src="js/jquery.validation/1.14.0/messages_zh.min.js"></script>
<style>
input.error {
	border: 1px solid #E6594E;
}
</style>
</head>
<body>
	<div class="container">

		<form id="addStudentForm">
			<br />
			<div class="form-group">
				<label for="sname">学生姓名：</label> <input type="text"
					class="form-control" id="sname" name="sname" placeholder="输入名称">
			</div>
			<div class="form-group">
				<label for="sage">学生年龄：</label> <input type="text"
					class="form-control" id="sage" name="sage" placeholder="输入年龄">
			</div>
			<div class="form-group">
				<label for="sclass">学生班级：</label> <input type="text"
					class="form-control" id="sclass" name="sclass" placeholder="输入年龄">
			</div>

			<div class="form-group">
				<button type="button" id="saveBtn" class="btn btn-success">提交</button>
				<button type="button" id="cancelBtn" class="btn btn-default">取消</button>
			</div>
		</form>
	</div>

	<script>
			var addStudent = function(){
				if(!check().form()){ 
					return;  
				}
				$.ajax({
					   type: "GET",
					   dataType: "json",
					   url: "/student/add",
					   data: {
							sname: $("#sname").val(),
							sage:$("#sage").val(),
						sclass:$("#sclass").val()
					   },
					   success: function(msg){
							//alert(msg);
							$('#cancelBtn').click();			
					   }
				});
			}
			
			$('#saveBtn').on('click', function(){
				addStudent();
			});
			
			$('#cancelBtn').on('click', function(){
				var index = parent.layer.getFrameIndex(window.name);
				
				parent.layer.close(index);
			});

			
			//校验字段是否正确 
            function check(){ 
                /*返回一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/ 
                return $("#addStudentForm").validate({ 
                            rules:{ 
                                sname:{ 
                                    required:true
                                },
                                sage:{ 
                                    required:true                                   
                                },
                                sclass:{ 
                                    required:true                                   
                                }
                            }, 
                            messages:{ 
                                sname:{ 
                                    required:""
                                },
                                sage:{ 
                                    required:""                                
                                },
                                sclass:{ 
                                    required:""                                
                                }
                            }     
                        }); 
            } 
			
		</script>
</body>
</html>